//
//  XYRockerView.m
//  FECCView
//
//  Created by 杨旭东 on 2017/9/11.
//  Copyright © 2017年 JackYang. All rights reserved.
//

#import "XYRockerView.h"

@interface XYRockerView ()

- (CGSize (^)())rockerSize;

- (CGSize (^)())arrowSize;

@property (nonatomic, assign) XYRockerViewType type;

@property (nonatomic, strong) UIButton *up;

@property (nonatomic, strong) UIButton *down;

@property (nonatomic, strong) UIButton *left;

@property (nonatomic, strong) UIButton *right;

@property (nonatomic, strong) XYRockerPointView *rockerPoint;

@property (nonatomic, strong) XYRobotRockerPointView *robotRockerPoint;


- (CGPoint (^)())upPoint;

- (CGPoint (^)())downPoint;

- (CGPoint (^)())leftPoint;

- (CGPoint (^)())rightPoint;

- (XYRockerView *(^)(CGPoint))touch;

@property (nonatomic, copy) void (^DirectionBlock)(XYRockerDirection, BOOL);

@property (nonatomic, assign) BOOL currentVertical;

@end

@implementation XYRockerView

+ (XYRockerView *(^)(CGPoint, XYRockerViewType))addXYRocker{
    return ^(CGPoint origin, XYRockerViewType type){
        XYRockerView *rocker = [[XYRockerView alloc] initWithOrigin:origin type:type];
        return rocker;
    };
}

- (void)rockerResult:(void (^)(XYRockerDirection, BOOL))result{
    self.DirectionBlock = result;
}

- (XYRockerView *(^)(XYRockerTrackType))trackMode{
    return ^(XYRockerTrackType mode){
        switch (mode) {
            case XYRockerTrackType_Horizontal:
                _up.enabled = _down.enabled = false;
                _left.enabled = _right.enabled = true;
                break;
                
            case XYRockerTrackType_Vertical:
                _up.enabled = _down.enabled = true;
                _left.enabled = _right.enabled = false;
                break;
                
            default:
                _up.enabled = _down.enabled = _left.enabled = _right.enabled = true;
                break;
        }
        
        _rockerPoint.trackType(mode);
        return self;
    };
}

- (instancetype)initWithOrigin:(CGPoint)origin type:(XYRockerViewType)type{
    if (self = [super init]) {
        if (type == XYRockerViewType_Normal) {
            self.frame = CGRectMake(origin.x, origin.y, self.rockerSize().width, self.rockerSize().height);
        }else{
            self.frame = CGRectMake(origin.x, origin.y, self.robotRockerSize().width, self.robotRockerSize().height);
        }
        self.type = type;
        
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.6f];
        self.layer.cornerRadius = self.bounds.size.width * .5f;
        self.layer.masksToBounds = YES;
        
        //Up
        _up = [[UIButton alloc] initWithFrame:CGRectMake(((type==XYRockerViewType_Normal?self.rockerSize().width:self.robotRockerSize().width) - self.arrowSize().width) * .5f - self.arrowSize().width, 0, self.arrowSize().width * 3, self.arrowSize().height + 15)];
        _up.imageEdgeInsets = UIEdgeInsetsMake(7.5, self.arrowSize().width, 7.5, self.arrowSize().width);
        NSString *path = [[NSBundle mainBundle] pathForResource:@"fecc_up@3x" ofType:@"png"];
        [_up setImage:[UIImage imageNamed:@"fecc_up"] forState:UIControlStateNormal];
        [_up setImage:[UIImage imageNamed:@"fecc_up_press"] forState:UIControlStateHighlighted];
        [_up setImage:[UIImage imageNamed:@"fecc_up_disable"] forState:UIControlStateDisabled];
        [_up addTarget:self action:@selector(upAction:) forControlEvents:UIControlEventTouchDown];
        [_up addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchUpInside];
        [_up addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchDragExit];
        [self addSubview:_up];
        
        //Down
        _down = [[UIButton alloc] initWithFrame:CGRectMake(((type==XYRockerViewType_Normal?self.rockerSize().width:self.robotRockerSize().width) - self.arrowSize().width) * .5f - self.arrowSize().width, self.rockerSize().height - 15 - self.arrowSize().height, self.arrowSize().width * 3, self.arrowSize().height + 15)];
        _down.imageEdgeInsets = UIEdgeInsetsMake(7.5, self.arrowSize().width, 7.5, self.arrowSize().width);
        [_down setImage:[UIImage imageNamed:@"fecc_down"] forState:UIControlStateNormal];
        [_down setImage:[UIImage imageNamed:@"fecc_down_press"] forState:UIControlStateHighlighted];
        [_down setImage:[UIImage imageNamed:@"fecc_down_disable"] forState:UIControlStateDisabled];
        [_down addTarget:self action:@selector(downAction:) forControlEvents:UIControlEventTouchDown];
        [_down addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchUpInside];
        [_down addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchDragExit];
        [self addSubview:_down];
        
        if (type == XYRockerViewType_Normal) {
            //left
            _left = [[UIButton alloc] initWithFrame:CGRectMake(0, (self.rockerSize().width - self.arrowSize().width) * .5f - self.arrowSize().width, self.arrowSize().height + 15, self.arrowSize().width * 3)];
            _left.imageEdgeInsets = UIEdgeInsetsMake(self.arrowSize().width, 7.5, self.arrowSize().width, 7.5);
            [_left setImage:[UIImage imageNamed:@"fecc_left"] forState:UIControlStateNormal];
            [_left setImage:[UIImage imageNamed:@"fecc_left_press"] forState:UIControlStateHighlighted];
            [_left setImage:[UIImage imageNamed:@"fecc_left_disable"] forState:UIControlStateDisabled];
            [_left addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchDown];
            [_left addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchUpInside];
            [_left addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchDragExit];
            [self addSubview:_left];
            
            //right
            _right = [[UIButton alloc] initWithFrame:CGRectMake(self.rockerSize().width - 15 - self.arrowSize().height, (self.rockerSize().width - self.arrowSize().width) * .5f - self.arrowSize().width, self.arrowSize().height + 15, self.arrowSize().width * 3)];
            _right.imageEdgeInsets = UIEdgeInsetsMake(self.arrowSize().width, 7.5, self.arrowSize().width, 7.5);
            [_right setImage:[UIImage imageNamed:@"fecc_right"] forState:UIControlStateNormal];
            [_right setImage:[UIImage imageNamed:@"fecc_right_press"] forState:UIControlStateHighlighted];
            [_right setImage:[UIImage imageNamed:@"fecc_right_disable"] forState:UIControlStateDisabled];
            [_right addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchDown];
            [_right addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchUpInside];
            [_right addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchDragExit];
            [self addSubview:_right];
        }
        
        __weak XYRockerView *weakSelf = self;
        if (type == XYRockerViewType_Normal) {
            UIImage *normal = [UIImage imageNamed:@"fecc_point"];
            UIImage *hignlight = [UIImage imageNamed:@"fecc_point_press"];
            CGSize size = self.rockerSize();
            _rockerPoint = XYRockerPointView.addXYRockerPoint(CGPointMake((size.width - 63) * .5f, (size.height - 63) * .5f))
                                            .background(normal, XYRockerPoint_Normal)
                                            .background(hignlight, XYRockerPoint_Highlight)
                                            .xyradius(65.0);
            [_rockerPoint breakLimit:^{
                [weakSelf resetAction:nil];
            }];
            [_rockerPoint reset:^(BOOL vertical) {
                weakSelf.currentVertical = vertical;
                [weakSelf resetAction:nil];
            }];
            [_rockerPoint direction:^(XYRockerDirection direction) {
                weakSelf.DirectionBlock(direction, NO);
            }];
            
            [self addSubview:_rockerPoint];
        }else{
            UIImage *normal = [UIImage imageNamed:@"fecc_point_robot"];
            UIImage *hignlight = [UIImage imageNamed:@"fecc_point_press_robot"];
            CGSize size = self.rockerSize();
            _robotRockerPoint = XYRobotRockerPointView.addXYRobotRockerPoint(CGPointMake(-1.5f, (size.height - 63) * .5f))
                                                      .background(normal, XYRockerPoint_Normal)
                                                      .background(hignlight, XYRockerPoint_Highlight)
                                                      .xyradius(65.0);
            [_robotRockerPoint breakLimit:^{
                [weakSelf resetAction:nil];
            }];
            [_robotRockerPoint reset:^() {
                [weakSelf resetAction:nil];
            }];
            [_robotRockerPoint direction:^(XYRockerDirection direction) {
                weakSelf.DirectionBlock(direction, NO);
            }];
            
            [self addSubview:_robotRockerPoint];
        }
    }
    return self;
}

- (CGSize (^)())rockerSize{
    return ^(){
        return CGSizeMake(130.0, 130.0);
    };
}

- (CGSize (^)())robotRockerSize{
    return ^(){
        return CGSizeMake(60.0, 130.0);
    };
}

- (CGSize (^)())arrowSize{
    return ^(){
        return CGSizeMake(14, 8);
    };
}

- (void)upAction:(UIButton *)sender{
    self.rockerPoint.highlighted = YES;
    self.touch(self.upPoint());
    self.DirectionBlock(_type==XYRockerViewType_Normal?XYRockerDirection_Up:XYRockerDirection_Forward, YES);
}

- (void)downAction:(UIButton *)sender{
    self.rockerPoint.highlighted = YES;
    self.touch(self.downPoint());
    self.DirectionBlock(_type==XYRockerViewType_Normal?XYRockerDirection_Down:XYRockerDirection_Retreat, YES);
}

- (void)leftAction:(UIButton *)sender{
    self.rockerPoint.highlighted = YES;
    self.touch(self.leftPoint());
    self.DirectionBlock(XYRockerDirection_Left, YES);
}

- (void)rightAction:(UIButton *)sender{
    self.rockerPoint.highlighted = YES;
    self.touch(self.rightPoint());
    self.DirectionBlock(XYRockerDirection_Right, YES);
}

- (void)resetAction:(UIButton *)sender{
    __weak XYRockerView *weakSelf = self;
    weakSelf.rockerPoint.highlighted = NO;
    weakSelf.touch(CGPointMake(_type==XYRockerViewType_Normal?(weakSelf.rockerSize().width - 63) * .5f:-1.5f, weakSelf.rockerSize().height * .5f - 31.5));
    weakSelf.DirectionBlock(_type==XYRockerViewType_Normal?(_currentVertical ? XYRockerDirection_Reset_Vertical : XYRockerDirection_Reset_Horizontal):XYRockerDirection_Reset_Move, NO);
}

- (CGPoint (^)())upPoint{
    return ^(){
        return CGPointMake(_type==XYRockerViewType_Normal?self.rockerSize().width * .5f - 31.5:0, - 9);
    };
}

- (CGPoint (^)())downPoint{
    return ^(){
        return CGPointMake(_type==XYRockerViewType_Normal?self.rockerSize().width * .5f - 31.5:0, self.rockerSize().height - 63 + 9);
    };
}

- (CGPoint (^)())leftPoint{
    return ^(){
        return CGPointMake(-9, self.rockerSize().height * .5f - 31.5);
    };
}

- (CGPoint (^)())rightPoint{
    return ^(){
        return CGPointMake(self.rockerSize().width - 63 + 9, self.rockerSize().height * .5f - 31.5);
    };
}

- (XYRockerView *(^)(CGPoint))touch{
    return ^(CGPoint point){
        __weak XYRockerView *weakSelf = self;
        
        [UIView animateWithDuration:.2 animations:^{
            if (weakSelf.type == XYRockerViewType_Normal) {
                weakSelf.rockerPoint.frame = CGRectMake(point.x, point.y, weakSelf.rockerPoint.bounds.size.width, weakSelf.rockerPoint.bounds.size.height);
            }else{
                weakSelf.robotRockerPoint.frame = CGRectMake(point.x, point.y, weakSelf.robotRockerPoint.bounds.size.width, weakSelf.robotRockerPoint.bounds.size.height);
            }
        }];
        return self;
    };
}

@end
